import { createRouter, createWebHistory } from 'vue-router'

// 导入布局组件
import DefaultLayout from '@/layouts/DefaultLayout.vue'

// 定义路由
const routes = [
  {
    path: '/',
    component: DefaultLayout,
    children: [
      {
        path: '',
        name: 'Home',
        component: () => import('@/pages/HomeView.vue'),
        meta: { title: '首页' }
      },
      {
        path: 'users',
        name: 'Users',
        component: () => import('@/pages/users/UserList.vue'),
        meta: { title: '用户管理' }
      },
      {
        path: 'users/:id',
        name: 'UserDetail',
        component: () => import('@/pages/users/UserDetail.vue'),
        meta: { title: '用户详情' }
      },
      {
        path: 'statistics',
        name: 'Statistics',
        component: () => import('@/pages/statistics/StatisticsView.vue'),
        meta: { title: '统计报表' }
      },
      {
        path: 'attractions',
        name: 'Attractions',
        component: () => import('@/pages/attractions/AttractionList.vue'),
        meta: { title: '景点管理' }
      },
      {
        path: 'activities',
        name: 'Activities',
        component: () => import('@/pages/activities/ActivityList.vue'),
        meta: { title: '活动管理' }
      },
      {
        path: 'orders',
        name: 'Orders',
        component: () => import('@/pages/orders/OrderList.vue'),
        meta: { title: '订单管理' }
      },
      {
        path: 'settings',
        name: 'Settings',
        component: () => import('@/pages/settings/SettingsView.vue'),
        meta: { title: '系统设置' }
      },

      {
        path: 'test/wechat-cloud',
        name: 'WechatCloudTest',
        component: () => import('@/pages/test/WechatCloudTest.vue'),
        meta: { title: '微信云测试' }
      },
      {
        path: 'profile',
        name: 'Profile',
        component: () => import('@/pages/profile/ProfileView.vue'),
        meta: { title: '个人中心' }
      },
      {
        path: 'account/settings',
        name: 'AccountSettings',
        component: () => import('@/pages/account/AccountSettings.vue'),
        meta: { title: '账号设置' }
      }
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/pages/auth/LoginView.vue'),
    meta: { title: '登录' }
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('@/pages/NotFound.vue'),
    meta: { title: '页面未找到' }
  }
]

// 创建路由实例
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  document.title = to.meta.title ? `${to.meta.title} - 贵州四季康养管理系统` : '贵州四季康养管理系统'
  
  // 这里可以添加权限验证逻辑
  // 例如：检查用户是否登录
  const isAuthenticated = localStorage.getItem('token')
  
  if (to.name !== 'Login' && !isAuthenticated) {
    // 未登录且访问非登录页面，重定向到登录页
    next({ name: 'Login' })
  } else if (to.name === 'Login' && isAuthenticated) {
    // 已登录且访问登录页面，重定向到首页
    next({ name: 'Home' })
  } else {
    next()
  }
})

export default router 